package no.ntnu.idi.deid.documentunits;

import java.util.ArrayList;
import java.util.List;

import no.ntnu.idi.deid.decision.validators.findPhrase.PhraseMatcher;
import no.ntnu.idi.deid.preprocessor.wordcount.CountManager;
import no.ntnu.idi.deid.searchresultunits.Tag;

/*
 * En setning består av mange tokens. En liste av tokens vil bli gitt av tokenizer til 
 * denne klassen med startindex og sluttindex, og man kan dermed lage setninger. Men husk,
 * hvis man gjør endrinder på spesifikke tokens i denne klasse, så vil man gjøre endringer 
 * på originaltokens også fordi tokens-lista kun består av pekere!
 * 
 */

public class Sentence extends ArrayList<Token> implements IDocumentUnits  {
	
	private static final long serialVersionUID = 1L;
	private int index; //The index of this sentence
	private List<Phrase> phraseList;
	private Section mParent;
	
	
	public Sentence(List<Token> allTokens,int index) {
		super(allTokens);
		if(allTokens.isEmpty()) {
			throw new IllegalArgumentException();
		}
		instansiate(index);
	}
	
	private void instansiate(int ind) {
		index = ind;
		phraseList = new ArrayList<Phrase>();
		setParent();
	}
	
	
	public List<Phrase> getPhraseList() {
		return phraseList;
	}
	
	public void setPhraseListFinalTags() {
		for(Phrase phr : phraseList) { 
			phr.setFinalTagsOnTokens();
		}
	}
	
	public void addPhrase(Phrase phrase) {
		if(phrase.getCategoryTag() != null) {
			phraseList.add(phrase);
		}
	}
	
	public boolean phraseListIsEmpty () {
		return phraseList.isEmpty();
	}
	
	private void setParent() {
		for(Token t: this) {
			t.setParent(this);
		}
	}
	
	public Section getSection() {
		return mParent;
	}
	
	public Document getDocument() {
		return mParent.getDocument();
	}

	public void setParent(Section parent) {
		this.mParent = parent;
	}

	public static Sentence makeSentence(String line){
		String words[] = line.split(" ");
		if(words.length==0 || line.isEmpty()){return null;}
		ArrayList<Token> tokenList = new ArrayList<Token>();
			for (int i = 0; i < words.length; i++) {
				if(!words[i].isEmpty()){
					Token temp = new Token(Document.getAndIncreaseTokenIndex(), words[i]);
					tokenList.add(temp);
				}
			}
			Sentence sentence = new Sentence(tokenList,Document.getAndIncreaseSentenceIndex());
			
		return sentence;
	}
	
	/**
	 * Returns a token that matches the str param
	 * @param str The string to be matched against the sentence.
	 * @return The token that matched, or null if no match was found.
	 */
	public Token getTokenFromString(String str) {
		String strippedString = Token.stripToken(str).toLowerCase();
		for(Token token: this) {
			if(token.getStripped().toLowerCase().equals(strippedString)) {
				return token;
			}
		}		
		return null;
	}
	
	public Token getTokenWithIndex(int index) throws IndexOutOfBoundsException {
			return get(index-getStartIndex());
	}
	
	public boolean tokenHasNext(Token token) {
		return (token.getIndex() < getEndIndex() && token.getIndex() >= getStartIndex());
			
	}
	
	public String getStripped() {
		String sentenceString = "";
		for(Token t: this){
			sentenceString += t.getStripped()+" ";			
		}
		return sentenceString.trim();
	}
	
	public String getOrginal() {
		String sentenceString = "";
		for(Token t: this){
			sentenceString += t.getOrginal()+" ";			
		}
		return sentenceString.trim();
	}
	
	public void addPhraseFromDocumentIndex(int startIndex, int endIndex, Tag tag) {
		Phrase phr = new Phrase();
		for(int i = startIndex - getStartIndex(); i<= endIndex-getStartIndex(); i++) {
			phr.add(get(i));
		}
		phr.setCategoryTag(tag);
		addPhrase(phr);
	}
	
	public boolean containsTag(Tag tag) {
		for(Token token: this) {
			if(token.containsCategoryTag(tag)) {
				return true;
			}
		}
		return false;
	}
	
	public int getEndIndex() {
		return getStartIndex()+size()-1;
	}

	public int getStartIndex() {
		return get(0).getIndex();
	}
	
	
	public String toString(){
		String str= "";
		for(Token t: this){
			str += t.getOrginal() + " ";
		}
		return str;
	}
	
	public int getIndex(){
		return index;
	}
	
	public boolean tokenExists(int i) {
		return (i < size());
	}
	
	public Phrase findPhrase(PhraseMatcher matcher, String phrase, int distance) {
		return matcher.findPhrase(this, phrase, distance);
	}
	
	public CountManager getCountManager() {
		return getDocument().getSentenceCountManager();
	}

}
